/**
 *
 *  Base64 encode / decode
 *
 */
const BASE64 = {
    KEY: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
    /**
     * 占位符
     */
    PLACEHOLDER: 'CB3',
    // private method for UTF-8 encoding
    _utf8_encode(string) {
        string = string.replace(/\r\n/g, '\n');
        var utftext = '';
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode (utftext) {
        var string = '';
        var i = 0;
        var c = 0;
        var c1 = 0;
        var c2 = 0;
        var c3 = 0;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } 
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } 
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    },

    /**
     * 将 base64URL 密文转为标准 base64 密文
     * @param {String} cipherText - 密文
     * @returns {String} - 标准 base64 密文
     */
    _toBase64(cipherText) {
        cipherText = cipherText.replace(new RegExp('\\-', 'gm'), '+').trim()
            .replace(new RegExp('_', 'gm'), '/').trim();
        var mode = cipherText.length % 4;
        if (mode === 0) {
            return cipherText;
        }
        for (var i = 0; i < 4 - mode; i++) {
            cipherText += '=';
        }
        return cipherText;
    }
    /**
     * 使用 base64 url 进行加密
     * 
     * @param {string} input - 明文
     * @returns {string} - 密文
     */
    ,encodeURL(input) {
        var str = this.encode(input);
        str = str.replace(new RegExp('\\+', 'gm'), '-').trim()
            .replace(new RegExp('/', 'gm'), '_')
            .trim()
            .replace(new RegExp('=', 'gm'), '')
            .trim();
        return str.trim();
    }

    /**
     * 解码 Base64 URL 标准密文
     * @param {String} input - 密文
     * @returns {String} - 明文
     */
    ,decodeURL(input) {
        input = this._toBase64(input);
        return this.decode(input).trim();
    }

    /**
     * 是否为 base64 url 标准密文
     * @param {String} input - 密文
     * @returns {Boolean} - 是否
     */
    ,isBase64URL(input) {
        if (typeof input !== 'string') {
            return false;
        }
        input = this._toBase64(input);
        return this.isBase64(input);
    }

    /**
     * public method for encoding
     * 
     * @param {string} input 
     * @returns {string}
     */
    ,encode(input) {
        var output = '';
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = this._utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
                this.KEY.charAt(enc1) + this.KEY.charAt(enc2) +
                this.KEY.charAt(enc3) + this.KEY.charAt(enc4);
        }
        return output;
    }

    /**
     * public method for decoding
     * 
     * @param {string} input 
     * @returns {string}
     */
    ,decode (input) {
        var output = '';
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
        while (i < input.length) {
            enc1 = this.KEY.indexOf(input.charAt(i++));
            enc2 = this.KEY.indexOf(input.charAt(i++));
            enc3 = this.KEY.indexOf(input.charAt(i++));
            enc4 = this.KEY.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 !== 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 !== 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = this._utf8_decode(output);
        return output;
    }

    /**
     * 是否为 base64 标准密文
     * @param {String} input - 密文
     * @returns {Boolean} - 是否
     */
    ,isBase64 (input) {
        if (typeof input !== 'string') {
            return false;
        }
        var reg = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
        return !!input.match(reg);
    }

    /**
     * 变种 URL 加密
     * @param {String} input - 待加密文本
     */
    ,encodeURL2 (input) {
        if (typeof input !== 'string') {
            throw new Error('参数类型错误');
        }
        if (input.length === 0) {
            throw new Error('参数不允许为空');
        }
        return this.PLACEHOLDER + this.encodeURL(input);
    }

    /**
     * 变种 URL 解码
     */
    ,decodeURL2 (input) {
        if (this.isBase64URL2(input)) {
            return this.decodeURL(input.substring(this.PLACEHOLDER.length));
        }
        return '';
    }

    /**
     * 是否为变种 URL 密文
     * @returns {Boolean} - 是否
     */
    ,isBase64URL2 (input) {
        return this.isBase64URL(input.substring(this.PLACEHOLDER.length));
    }

}

export default BASE64;
    // win.BASE64 = {
    //     encodeURL: encodeURL,
    //     decodeURL: decodeURL,
    //     isBase64URL: isBase64URL,
    //     encode: encode,
    //     decode: decode,
    //     isBase64: isBase64,
    //     encodeURL2: encodeURL2,
    //     decodeURL2: decodeURL2,
    //     isBase64URL2: isBase64URL2
    // }
